package com.lanlengran.musicplayer.beans;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.lanlengran.musicplayer.utils.Utils;

import java.util.ArrayList;

/**
 * Created by dobest on 2016/6/8.
 */
public class MusicInfo {
    private long id; // 歌曲ID 3
    private String title; // 歌曲名称 0
    private String album; // 专辑 7
    private long albumId;//专辑ID 6
    private String displayName; //显示名称 4
    private String artist; // 歌手名称 2
    private long duration; // 歌曲时长 1
    private long size; // 歌曲大小 8
    private String url; // 歌曲路径 5
    private String lrcTitle; // 歌词名称
    private String lrcSize; // 歌词大小


    //    public static ArrayList<MusicInfo> queryAllMusics(Context context) {
//        ArrayList<MusicInfo> musiclistResult = new ArrayList<MusicInfo>();
//        ContentResolver cr = context.getContentResolver();
//        Cursor musics = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
//                new String[]{
//                        MediaStore.Audio.Media._ID, // int
//                        MediaStore.Audio.Media.TITLE,
//                        MediaStore.Audio.Media.ARTIST,
//                        MediaStore.Audio.Media.ALBUM,
//                        MediaStore.Audio.Media.DURATION,
//                        MediaStore.Audio.Media.DATA, // String
//                        MediaStore.Audio.Media.DISPLAY_NAME, // String
//                        MediaStore.Audio.Media.MIME_TYPE // String
//                },
//                MediaStore.Audio.Media.IS_MUSIC + " = 1 AND "
//                        + MediaStore.Audio.Media.DURATION + " > 10000",
//                null,
//                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
//        Log.d("", "musics : " + musics.getCount());
//
//        musics.moveToFirst();
//        while (!musics.isAfterLast()) {
//            MusicInfo temp = new MusicInfo();
//            temp._ID = musics.getInt(musics.getColumnIndex(MediaStore.Audio.Media._ID));
//            temp.ALBUM = musics.getString(musics.getColumnIndex(MediaStore.Audio.Media.ALBUM));
//            temp.ARTIST = musics.getString(musics.getColumnIndex(MediaStore.Audio.Media.ARTIST));
//            temp.DATA = musics.getString(musics.getColumnIndex(MediaStore.Audio.Media.DATA));
//            temp.DURATION = musics.getLong(musics.getColumnIndex(MediaStore.Audio.Media.DURATION));
//            temp.TITLE = musics.getString(musics.getColumnIndex(MediaStore.Audio.Media.TITLE));
//            temp.isExist = true;
//
//            musiclistResult.add(temp);
//            musics.moveToNext();
//        }
//        musics.close();
//        return musiclistResult;
//    }
    public static ArrayList<MusicInfo> queryMusics(Context context) {
        //     public static List getMp3Infos(Context context){
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        ArrayList<MusicInfo> mp3Infos = new ArrayList();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            long id = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
            String title = cursor.getString((cursor
                    .getColumnIndex(MediaStore.Audio.Media.TITLE))); // 音乐标题
            String artist = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 艺术家
            String album = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM)); // 专辑
            String displayName = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
            long albumId = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
            long duration = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长
            long size = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
            String url = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
            int isMusic = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐
            if (isMusic != 0) { // 只把音乐添加到集合当中
                MusicInfo mp3Info = new MusicInfo();
                mp3Info.setId(id);
                mp3Info.setTitle(title);
                mp3Info.setArtist(artist);
                mp3Info.setAlbum(album);
                mp3Info.setDisplayName(displayName);
                mp3Info.setAlbumId(albumId);
                mp3Info.setDuration(duration);
                mp3Info.setSize(size);
                mp3Info.setUrl(url);
                mp3Infos.add(mp3Info);
            }
            cursor.moveToNext();
        }
        return mp3Infos;
        //     }

    }

    public static MusicInfo queryMusic(String name, Context context) {
        ArrayList<MusicInfo> musicInfos = MusicInfo.queryMusics(context);
        for (MusicInfo m : musicInfos) {

            String data = m.getUrl();
            if (name.length() > data.length()) {
                continue;
            }
            String lastName = data.substring(data.length() - name.length(), data.length());
            Utils.log_i("name=" + name);
            Utils.log_i("lastName=" + lastName);
            if (name.equals(lastName)) {
                return m;
            }
        }
        return null;
    }
    public static String getAlbumArt(Context context,int album_id) {
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[] { "album_art" };
        Cursor cur = context.getContentResolver().query(
                Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)),
                projection, null, null, null);
        String album_art = null;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_art = cur.getString(0);
        }
        cur.close();
        cur = null;
        return album_art;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public void setAlbumId(long albumId) {
        this.albumId = albumId;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setLrcTitle(String lrcTitle) {
        this.lrcTitle = lrcTitle;
    }

    public void setLrcSize(String lrcSize) {
        this.lrcSize = lrcSize;
    }

    public String getUrl() {
        return url;
    }

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getAlbum() {
        return album;
    }

    public long getAlbumId() {
        return albumId;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getArtist() {
        return artist;
    }

    public long getDuration() {
        return duration;
    }

    public long getSize() {
        return size;
    }

    public String getLrcTitle() {
        return lrcTitle;
    }

    public String getLrcSize() {
        return lrcSize;
    }
}
